package com.bytenebula.asteroid.utils;

import cn.hutool.json.JSONObject;

import java.util.Map;

public class JdbcBuilder {

    public static String buildJdbcUrl(String db_type, String url_tpl, String split_str,
                                      String host, Integer port, String db_name, String parameters) {
        String url = url_tpl.replace("{host}", host);
        url = url.replace("{port}", port.toString());
        url = url.replace("{database}", db_name);
        StringBuilder params = new StringBuilder();
        if (parameters != null) {
            JSONObject jsonobj = new JSONObject(parameters);
            for (Map.Entry<String, Object> entry : jsonobj.entrySet()) {
                params.append(String.format("%s%s=%s", split_str, entry.getKey(), entry.getValue().toString()));
            }
        }
        url = url.replace("{params}", params.toString());
        if (url.endsWith("?")) {
            url = url.substring(0, url.length()-1);
        }
        return url.trim();
    }

}
